define(['angularAMD','moment'], function (angularAMD,moment) {
    angularAMD.service('Andon02Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater', '$timeout',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater, $timeout){
            /* 
                slightBtnItem: 當前點擊的安燈按鈕資訊 (Object)
                machine_no: 當前點擊的設備編號 (string)
                callback: 刷新安燈按鈕 (function)
            */
            this.init = function(slightBtnItem, machine_no, callback){
                // 20220624 modify by Alan for#100246 : 帳號密碼驗證改為IC卡掃刷
                function openDialog() {
                    $mdDialog.dialog(
                        'module/moduleMSG/program/Andon02/Andon02.html',
                        function(dialog){
                            return {
                                css_url:'module/moduleMSG/program/Andon02/Andon02.css',
                                AndonItem: {
                                    ic_card: '', // ic卡號
                                    user_acc: '', // 用戶
                                },
                                back: function() {
                                    dialog.hide();
                                },
                                confirm: function() {
                                    if(dialog.AndonItem.user_acc === '') {
                                        $rootScope.showAlert($filter('translate')('请输入ID卡或用户编号确定操作人身份'));
                                        return;
                                    }

                                    user_information_get(dialog.AndonItem.user_acc, function() {
                                        andon_eqp_status_process(dialog)
                                    });
                                },
                                ICCardScanner: function () {
                                    $rootScope.OpenScanner({
                                        after: function (result) {
                                            $timeout(function () {
                                                dialog.AndonItem.ic_card = result.text;
                                                dialog.searchICCard();
                                            });
                                        },
                                        exception: function () {
                                            //目前裝置無法使用相機
                                            $rootScope.showAlert($filter('translate')('B0214.showAlert.column3'));
                                        },
                                        orientation: 'landscape'
                                    });
                                },
                                searchICCard: function (event) {
                                    if (!event || event.keyCode == 13) {
                                        if (dialog.AndonItem.ic_card != '') {
                                            var content = {
                                                IcNo: dialog.AndonItem.ic_card
                                            }
                                            $MMWService.sendToServer({
                                                uri: 'ServicesSTD.Module_SYS.ic_card_info_get',
                                                content: content,
                                                success: function (data) {
                                                    if (data.UserNo != null && data.PassWord != null) {
                                                        dialog.AndonItem.user_acc = data.UserNo;
                                                    }
                                                    else {
                                                        $rootScope.showAlert($filter('translate')('Login.error.ic_card_error'));
                                                        dialog.AndonItem.ic_card = '';
                                                        document.getElementById('ic_card').focus();
                                                    }
                                                },
                                                error: function (data) {
                                                    $rootScope.showAlert(data.Message);
                                                    dialog.AndonItem.ic_card = '';
                                                    document.getElementById('ic_card').focus();
                                                }
                                            });
                                        } else {
                                            $rootScope.showAlert($filter('translate')('B0202.showAlert.column7'));
                                            dialog.AndonItem.ic_card = '';
                                            document.getElementById('ic_card').focus();
                                        }
                                    }
                                },
                            };
                        }
                    );
                }

                function user_information_get(employee_no, callback) {
                    var content = {
                        machine_no : 'A',
                        employee_no : employee_no,
                        report_datetime : new Date(),
                        return_type : '10'
                    };

                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SYS.user_information_get',
                        content : content,
                        success : function(){
                            callback && callback();
                        },
                        error:function(){
                            $rootScope.showAlert('输入的用户错误，请重新输入。');
                        }
                    });
                }

                function andon_eqp_status_process(dialog) {
                    var eqp_status_list = [{
                        equipment_no: machine_no, 		                        // 設備編號	    
                        slight_type_no: slightBtnItem.slight_type_no,		    // 安燈分類狀態 
                        user_acc: dialog.AndonItem.user_acc,                    // 輸入帳號
                        user_pw: 'SSO' + dialog.AndonItem.user_acc,             // magic
                        compare_pc_in: slightBtnItem.compare_pc_in,             // 安燈任務處理人是否為接收人之一選項 -> 0: 否 ; 1: 是
                        compare_rc_and_pc: slightBtnItem.compare_rc_and_pc,     // 安燈任務處理人與接收人是否一致選項 -> 0: 否 ; 1: 是
                        receiver: slightBtnItem.receiver,                       // 安燈任務接收人 	
                    }];

                    var content = {
                        slight_status: slightBtnItem.slight_status,
                        write_info : slightBtnItem.write_info,
                        write_start: slightBtnItem.write_start,
                        receive_project: slightBtnItem.receive_project,
                        //20210423 Modify by德義家興for#78800: [标准][安灯/讯息][DEV]安灯与设备稼动联动机制
                        change_eqp_status :slightBtnItem.change_eqp_status,
                        eqp_status:slightBtnItem.eqp_status,
                        // 20220621 add by zhuyc0412 for#0100246: [3.4.1patch][安灯]安灯模块相关优化建议及相关议题搬迁
                        eqp_process_status: slightBtnItem.eqp_process_status,//0-7对应安灯后设备将要修改成的状态
                        eqp_status_back:slightBtnItem.eqp_status_back,
                        eqp_status_list: eqp_status_list
                    };
                        
                    // 安燈寫入
                    $MMWService.sendToServer({
                        uri: "ServicesSTD.Module_MSG.andon_eqp_status_process",
                        content: content,
                        success: function (data) {
                            $rootScope.showToast($filter("translate")("common.msg.success"));
                            if (callback) callback();
                            //20210423 Modify by德義家興for#78800: [标准][安灯/讯息][DEV]安灯与设备稼动联动机制
                            dialog.hide();
                        },
                    });  
                }

                openDialog();

            };
        }]);
});
